//
//  YQAddressManageVC.m
//  MuYing
//
//  Created by tbb on 2019/7/31.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "YQAddressManageVC.h"
#import "YQAddressAddVC.h"
@interface YQAddressManageVC ()

@property (strong, nonatomic) NSMutableArray *addressArr;

@end

@implementation YQAddressManageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = kYQLS(@"我的收货地址");
    self.rightItemTitle = kYQLS(@"新增");
    self.addressArr = NSMutableArray.new;

}

- (void)setupRefresh
{
    self.listTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getUserAddressApi:YES];
    }];

    self.listTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.addressArr.count < 10) {
            [self.listTableView.mj_footer endRefreshing];
            return ;
        }
        self.page ++;

        [self getUserAddressApi:NO];
    }];
    [self.listTableView.mj_header beginRefreshing];
    self.listTableView.mj_header.automaticallyChangeAlpha = YES;

}
#pragma amrk - POST查询用户收货地址列表
- (void)getUserAddressApi:(BOOL)isReload
{
    if (isReload) {
        self.page = 1;
    }
    NSMutableDictionary *dic = NSMutableDictionary.new;

    dic[@"page"] = @(self.page);
    dic[@"limit"] = @(10);

    [self.addressArr removeAllObjects];
    [YQPNetWorkingManager sendPostRequest:kPOST_QUERY_USER_ADDRESS_LIST_URL parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQPAddressModel *addressModel = [YQPAddressModel mj_objectWithKeyValues:responseObject.data];
        [self.addressArr addObjectsFromArray:addressModel.list];
        [self.addressArr sortUsingComparator:^NSComparisonResult(YQPAddressEntityModel   *obj1, YQPAddressEntityModel   *obj2) {
            return obj1.isDefault < obj2.isDefault;
        }];
        [self.listTableView reloadData];
        [self.listTableView.mj_header endRefreshing];
        [self.listTableView.mj_footer endRefreshing];

    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        [self.listTableView.mj_header endRefreshing];
        [self.listTableView.mj_footer endRefreshing];
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setupRefresh];
}

-(void)rightItemAction:(UIButton *)sender {
    YQAddressAddVC *addVC = [[YQAddressAddVC alloc] init];
    kYQPUSH(addVC);
}

-(void)setupView {
    [super setupView];
    self.isNavShadow = YES;
    self.listTableView.backgroundColor = HexColor(0xFAFAFA);
    kYQREGISTERTABLEVIEWNIBCELL(@"MYShopSalesOrderAddressCell")
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.addressArr.count;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YQPAddressEntityModel *model = nil;
    if (indexPath.section < self.addressArr.count) {
        model = self.addressArr[indexPath.section];
    }

    MYShopSalesOrderAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYShopSalesOrderAddressCell"];

    [cell.rightBtn setTitle:kYQLS(@"编辑") forState:(UIControlStateNormal)];
    cell.rightBtn.tag = indexPath.section;
    [cell.rightBtn addTarget:self action:@selector(editAddressModel:andModel:) forControlEvents:UIControlEventTouchUpInside];
    [cell setModel:model];

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 83;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YQPAddressEntityModel *model = self.addressArr[indexPath.section];
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectAddressEntityModel:)]) {
        [self.delegate selectAddressEntityModel:model];
        [self.navigationController popViewControllerAnimated:YES];

    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 8;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

#pragma mark - 侧滑删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    YQPAddressEntityModel *model = self.addressArr[indexPath.section];

    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self removeAddressModel:model and:indexPath];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kYQLS(@"删除");
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end
- (void)removeAddressModel:(YQPAddressEntityModel *)model and:(NSIndexPath *)indexPath
{
    
    NSString *url = [NSString stringWithFormat:@"%@%@",kGET_DELETE_RECEIVE_ADDRESS_URL,model.addressId];
    [YQPNetWorkingManager sendGetRequest:url parameters:@{} success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
         [MBProgressHUD showMessage:kYQLS(@"删除成功")];
        [self getUserAddressApi:YES];
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        
    }];
}

#pragma mark - 编辑按钮事件
- (void)editAddressModel:(UIButton *)button andModel:(YQPAddressEntityModel *)model
{
    YQAddressAddVC *addVC = [[YQAddressAddVC alloc] init];
    addVC.entityModel = self.addressArr[button.tag];
    addVC.isEdit = YES;
    kYQPUSH(addVC);
}
@end
